﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace VMCCryptoLib.HashEncryption
{
    public class SHA384Hash
    {
    	#region --- Method to hash a string and a file with SHA384 algorithm
        /// <summary>
        /// Encrypts a string using the SHA384(Secure Hash Algorithm) algorithm.
        /// This works in the same manner as MD5, providing 384bit encryption.
        /// </summary>
        /// <param name="Data">A string containing the data to encrypt.</param>
        /// <returns>A string containing the string, encrypted with the SHA384 algorithm.</returns>
        public static string HashString(string input)
        {
            SHA384 sha = new SHA384Managed();
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(input));

            StringBuilder stringBuilder = new StringBuilder();
            foreach (byte b in hash)
            {
                stringBuilder.AppendFormat("{0:x2}", b);
            }
            return stringBuilder.ToString();
        }
        
        /// <summary>
        /// Method to hash a file with SHA384 algorithm
        /// </summary>
        /// <param name="fileName">path of file</param>
        /// <returns>hashed string</returns>
        public static string ThisFile(string fileName)
        {
        	string rs = string.Empty;
        	
        	SHA384 sha = new SHA384Managed();
        	FileStream fileStream = new FileStream(fileName,FileMode.Open, FileAccess.Read);
        	byte[] hash = sha.ComputeHash(fileStream);
        	StringBuilder strBuilder = new StringBuilder();
        	foreach(byte b in hash)
        	{
        		strBuilder.AppendFormat("{0:x2}", b);
        	}
        	
        	return rs = strBuilder.ToString();
        }
        #endregion
    }
}
